/*merge sort package */
package mysort

import (
	"log"
	"reflect"
)

func Merge(value interface{}) (interface{}, error) {
	re := reflect.TypeOf(value)
	switch value.(type) {
	case string:
		log.Println("string")
	case []string:
		log.Println("[]string")
	case []int:
		log.Println("[]int")
	case []int64:
		log.Println("[]int64")

	default:
		log.Println("result:", re)
	}
	return "ok", nil
}

func Mergeint64(ary []int64) []int64 {

	l := len(ary)
	if l <= 1 {
		return ary
	}
	num := l / 2
	log.Println("num:", num)
	left := Mergeint64(ary[:num])
	right := Mergeint64(ary[num:])
	return merge_int64(left, right)
}
func merge_int64(l, r []int64) []int64 {
	var result []int64
	i, j := 0, 0
	for i < len(l) && j < len(r) {
		if l[i] < r[j] {
			result = append(result, l[i])
			i++
		} else {
			result = append(result, r[j])
			j++
		}

	}
	result = append(result, l[i:]...)
	result = append(result, r[j:]...)
	return result
}

func MergeInt(ary []int) []int {
	l := len(ary)
	if l <= 1 {
		return ary
	}
	num := l / 2
	log.Println("num:", num)
	left := MergeInt(ary[:num])
	right := MergeInt(ary[num:])
	return merge_int(left, right)
}

func merge_int(l, r []int) []int {
	var result []int
	i, j := 0, 0
	for i < len(l) && j < len(r) {
		if l[i] < r[j] {
			result = append(result, l[i])
			i++
		} else {
			result = append(result, r[j])
			j++
		}

	}
	result = append(result, l[i:]...)
	result = append(result, r[j:]...)
	return result
}
